﻿using ELTE.SEM.Data.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;
using WinSCP;
using Renci.SshNet;
using Renci.SshNet.Sftp;

namespace ELTE.SEM.Data.Connectors
{
    public class FolderDataConnector
    {
        //public List<SemDataViewModel> GetDirectoriesSCP(Session session, string path)
        //{
        //    List<SemDataViewModel> result = new List<SemDataViewModel>();
        //    RemoteDirectoryInfo info = session.ListDirectory(path);
        //    foreach (RemoteFileInfo item in info.Files.Where(i => !i.Name.StartsWith(".")))
        //    {
        //            result.Add(new SemDataViewModel { Name = item.Name, ParentFolder = path, IsDirectory=item.IsDirectory });
        //            if (item.IsDirectory)
        //            {
        //                string subfolderPath = path;
        //                if (!subfolderPath.EndsWith("/"))
        //                {
        //                    subfolderPath += "/";
        //                }
        //                result.AddRange(GetDirectories(session, String.Concat(subfolderPath, item.Name, "/")));
        //            }
        //    }
        //    return result;
        //}

        public List<SemDataViewModel> GetDirectoriesSSH(SftpClient client, string path)
        {
            List<SemDataViewModel> folders = new List<SemDataViewModel>();
            try
            {
                var filesInPath = client.ListDirectory(path).Where(f => !f.Name.StartsWith("."));

                foreach (var item in filesInPath)
                {
                    if (item.IsDirectory || item.IsSymbolicLink || item.FullName.ToLower().EndsWith("tif") || item.FullName.ToLower().EndsWith("tif"))
                    {
                        string folderFullName = String.Concat(path, "/", item.Name);
                        folders.Add(
                            new SemDataViewModel
                            {
                                IsDirectory = item.IsDirectory || item.IsSymbolicLink,
                                Name = item.Name,
                                ParentFolder = path,
                                FullName = item.FullName,
                            });
                        if (item.IsDirectory || item.IsSymbolicLink)
                        {
                            folders.AddRange(GetDirectoriesSSH(client, folderFullName));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //TODO: error logging
            }

            return folders;
        }

        /// <summary>
        /// Az adott könyvtárhoz tartozó összes adott formátumú file nevét visszaadja
        /// </summary>
        /// <param name="client"></param>
        /// <param name="model"></param>
        /// <param name="extension"></param>
        /// <returns></returns>
        public List<String> GetFilesInFolder(SftpClient client, SemFolderViewModel model, string rootPath, string extension)
        {
            List<string> filesList = new List<string>();
            try
            {
                if (!client.IsConnected)
                {
                    client.Connect();
                }
                IEnumerable<SftpFile> filesInFolder = client.ListDirectory(rootPath+model.FullPath)
                    .Where(f => 
                        f.IsRegularFile
                        && f.Name.EndsWith(extension)
                    );
                filesList = filesInFolder.Select(f => f.FullName).ToList();
            }
            catch (Exception ex)
            {
                //TODO: log error
            }

            return filesList;
        }

        public Stream GetFileStream(SftpClient client, SemFolderViewModel model, string fileName)
        {
            Stream fileDataStream = null;
            try
            {
                client.DownloadFile(model.GetPath() + fileName, fileDataStream);
            }
            catch (Exception ex)
            {
                //TODO: error log
            }
            return fileDataStream;
        }
    }
}
